package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.mall.vo.CategoryVo;
import com.cskaoyan.bean.admin.mall.vo.L1Vo;
import com.cskaoyan.service.admin.CategoryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Name : CategoryController.java
 * @Time : 2021/8/11 21:28
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @RequiresPermissions(value = {"*", "admin:category:list"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo list() {
        List<CategoryVo> list = categoryService.list();
        return BaseRespVo.ok(list);
    }

    @RequiresPermissions(value = {"*", "admin:category:l1"}, logical = Logical.OR)
    @RequestMapping("l1")
    public BaseRespVo l1() {
        List<L1Vo> l1Vos = categoryService.l1();
        return BaseRespVo.ok(l1Vos);
    }

    @RequiresPermissions(value = {"*", "admin:category:delete"}, logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CategoryVo categoryVo) {
        categoryService.delete(categoryVo);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*", "admin:category:create"}, logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody CategoryVo categoryVo) {
        if (categoryVo.getPid() == 0 && categoryVo.getLevel().equals("L2")) {
            return BaseRespVo.fail("二级类目的父类目不能为0");
        }
        CategoryVo categoryVo1 = categoryService.create(categoryVo);
        return BaseRespVo.ok(categoryVo1);
    }


    @RequiresPermissions(value = {"*", "admin:category:update"}, logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody CategoryVo categoryVo) {
        if (categoryVo.getLevel().equals("L2") && categoryVo.getPid() == null) {
            return BaseRespVo.fail("二级类目的父类目不能为空");
        }
        categoryService.update(categoryVo);
        return BaseRespVo.ok();
    }

}
